package com.example.webrestfulcrud.component;

import org.springframework.util.ObjectUtils;
import org.springframework.web.servlet.HandlerInterceptor;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * 拦截未登陆的用户
 */
public class MyInterceptor implements HandlerInterceptor {

    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {

        Object isLogin = request.getSession().getAttribute("isLogin");

        if (!ObjectUtils.isEmpty(isLogin)) {
            return true;
        }
        request.setAttribute("msg","您还没有登陆，请先登陆");
        request.getRequestDispatcher("/login.html").forward(request,response);
        return false;
    }
}
